//
//  ContainerViewController.m
//  收纳
//
//  Created by  on 2022/10/28.
//

#import "ContainerViewController.h"
#import "GoodsCell.h"
#import "ContainerHeader.h"
#import "AddViewController.h"

@interface ContainerViewController ()
@property (nonatomic, strong) NSArray <Goods *>*array;
@end

@implementation ContainerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (void)initUI{
    self.title = @"容器";
    [self.rightBtn setTitle:@"" forState:UIControlStateNormal];
    [self.rightBtn setImage:[UIImage imageNamed:@"添加_黄_圆"] forState:UIControlStateNormal];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectionView.collectionViewLayout = flowLayout;
    [self.collectionView registerNib:[GoodsCell nib] forCellWithReuseIdentifier:[GoodsCell ID]];
    [self.collectionView registerNib:[ContainerHeader nib] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[ContainerHeader ID]];
    self.collectionView.c_edges.equalTo(self.view);
    
}
- (void)loadData{
    [GoodsTool find:NO name:self.model.name complete:^(NSMutableArray<Goods *> * _Nonnull result) {
        self.array = result;
        [self.collectionView reloadData];
    }];
}
- (void)setModel:(Goods *)model{
    _model = model;
    [self loadData];
}
#pragma mark - 导航栏添加按钮
- (void)rightBtnAction{
    AddViewController *vc = [AddViewController new];
    vc.model = [Goods new];
    vc.model.isContainer = NO;
    vc.model.container = self.model.name;
    vc.model.stamp = [[NSDate currentTimeStamp:@"yyyy-MM-dd HH:mm:ss"] integerValue];
    [self push:vc :YES];
}
- (void)action:(NSInteger)tag{
    if (tag == 201) {
        AddViewController *vc = [AddViewController new];
        NSDictionary *dict = [self.model mj_keyValues];
        vc.model = [Goods mj_objectWithKeyValues:dict];
        [self push:vc :YES];
    }else{
        [GoodsTool delete:self.model complete:^(BOOL success) {
            [self backBtnAction];
        }];
    }
}
- (void)addNotify{
    [NotifyTool add:self :@selector(loadData) :notifyGoodsUpdate];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.array.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GoodsCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[GoodsCell ID] forIndexPath:indexPath];
    cell.model = self.array[indexPath.row];
    return  cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    ContainerHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[ContainerHeader ID] forIndexPath:indexPath];
    header.model = self.model;
    WS(self)
    header.block = ^(NSInteger tag) {
        [weakself action:tag];
    };
    return  header;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    AddViewController *vc = [AddViewController new];
    vc.model = self.array[indexPath.row];
    [self push:vc :YES];
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  CGSizeMake(SCREEN_WIDTH - 40,124);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(0, 20, 20, 20);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(SCREEN_WIDTH,330.0 / 375 * SCREEN_WIDTH + 137);
}
@end
